package com.itheima.itcasthealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.itcasthealth.common.Result;
import com.itheima.itcasthealth.entiry.Member;
import com.itheima.itcasthealth.entiry.Order;
import com.itheima.itcasthealth.entiry.OrderDto;
import com.itheima.itcasthealth.entiry.Setmeal;
import com.itheima.itcasthealth.mapper.MemberMapper;
import com.itheima.itcasthealth.mapper.OrdersMapper;
import com.itheima.itcasthealth.mapper.SetmealMapper;
import com.itheima.itcasthealth.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result submit(Map map) {
        String idCard = (String) map.get("idCard");
        String setmealId = (String) map.get("setmealId");
        String name = (String) map.get("name");
        String orderDate = (String) map.get("orderDate");
        String sex = (String) map.get("sex");
        String telephone = (String) map.get("telephone");

        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getPhoneNumber,telephone);
        Member member = memberMapper.selectOne(wrapper);
        if (member != null){
            if (!member.getIdCard().equals(idCard)){
                return  new Result(false,"身份证号码错误");
            }
            if (!member.getName().equals(name)){
                return  new Result(false,"名字错误");
            }
        }else {
            member = new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            member.setName(name);
            member.setSex(sex);
            memberMapper.insert(member);
    }
        Order order = new Order();
        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
            order.setOrderDate(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        order.setMemberId(member.getId());

        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        ordersMapper.insert(order);

        return new Result(true,"预约成功",order.getId());
    }

    @Cacheable(value = "Order",key = "#id")
    @Override
    public Result findById(Integer id) {
        Order order = ordersMapper.selectById(id);
        Member member = memberMapper.selectById(order.getMemberId());
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId());
        OrderDto orderDto = new OrderDto();
        BeanUtils.copyProperties(order,orderDto);
        orderDto.setMember(member.getName());
        orderDto.setSetmeal(setmeal.getName());
        return new Result(true,"查询成功",orderDto);
    }
}
